﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================

using DevExpress.XtraBars.ToolbarForm;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.BandedGrid;
using XCI.Extensions;
using XCI.Helper;
using XCI.Setting.Controls;
using XCI.Windows;

namespace XCI.Setting
{
    public partial class FrmSettingGridForm : ToolbarForm
    {
        private GridControlPlus _gridControlPlus;

        public FrmSettingGridForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (WinFormHelper.IsDesignMode()) WinFormHelper.InitDesignFont();
        }

        public void Init(GridControlPlus gridControlPlus)
        {
            this._gridControlPlus = gridControlPlus;
            this.barId.EditValue = gridControlPlus.Id;
            tabBands.PageVisible = gridControlPlus.IsBandedGridView;
            InitControls();
            TabDelayLoad.Init(tabPane1, name =>
            {
                switch (name)
                {
                    case nameof(tabGeneral):
                        _settingGridGeneral1.Init(_gridControlPlus.CurrentGridView);
                        break;
                    case nameof(tabColumns):
                        gridColumns1.Init();
                        break;
                    case nameof(tabBands):
                        gridBands1.Init(_gridControlPlus.CurrentBandedGridView);
                        break;
                    case nameof(tabGroupSummary):
                        _settingGridGroupSummary1.Init(_gridControlPlus.CurrentGridView);
                        break;
                    case nameof(tabAppearance):
                        gridAppearance1.Init(_gridControlPlus.MainView.Appearance);
                        break;
                    case nameof(tabExpression):
                        gridFormatCondition1.Init();
                        break;
                    case nameof(tabTemplate):
                        gridTemplate1.Init(_gridControlPlus.SettingManager, "grid_:_");
                        break;
                    case nameof(tabAdvanced):
                        gridAdvanced1.SelectedObject = _gridControlPlus.MainView;
                        break;
                }
            });
        }

        private void InitControls()
        {
            #region gridColumns1
            gridColumns1.GetModelInfo = () => _gridControlPlus.ApiModelInfo;
            gridColumns1.GetColumns = () =>
            {
                var colList = new List<GridColumn>();
                if (!_gridControlPlus.IsGridView) return colList;
                var hiddenList = new List<GridColumn>();
                foreach (GridColumn item in _gridControlPlus.CurrentGridView.Columns)
                {
                    if (item.Visible)
                    {
                        colList.Add(item);
                    }
                    else
                    {
                        hiddenList.Add(item);
                    }
                }

                colList.Sort((a, b) => a.VisibleIndex.CompareTo(b.VisibleIndex));
                colList.AddRange(hiddenList);
                return colList;
            };
            gridColumns1.AddColumn = () =>
            {
                if (!_gridControlPlus.IsGridView) return;
                GridColumn col = _gridControlPlus.IsBandedGridView ? new BandedGridColumn() : new GridColumn();
                col.Name = $"col_{StringHelper.Guid()}";
                col.Visible = true;
                col.OptionsColumn.AllowEdit = false;
                // col.AppearanceCell.Options.UseTextOptions = true;
                // col.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
                // col.AppearanceHeader.Options.UseTextOptions = true;
                // col.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
                _gridControlPlus.CurrentGridView.Columns.Add(col);
            };
            gridColumns1.RemoveColumn = col =>
            {
                if (!_gridControlPlus.IsGridView) return;
                _gridControlPlus.CurrentGridView.Columns.Remove((GridColumn)col);
            };
            gridColumns1.AutoBuildColumn = () => _gridControlPlus.AutoBuildColumn();
            #endregion

            #region gridFormatCondition1
            gridFormatCondition1.GetConditions = () => _gridControlPlus.MainView.FormatConditions;
            gridFormatCondition1.AddCondition = () =>
            {
                var condition = new StyleFormatCondition { Condition = FormatConditionEnum.Expression };
                _gridControlPlus.MainView.FormatConditions.Add(condition);
                return condition;
            };
            gridFormatCondition1.GetColumnInfos = () =>
            {
                var colInfos = new List<ColumnInfo>();
                if (!_gridControlPlus.IsGridView) return colInfos;
                foreach (GridColumn col in _gridControlPlus.CurrentGridView.Columns)
                {
                    string name = string.IsNullOrEmpty(col.Caption) ? col.FieldName : col.Caption;
                    colInfos.Add(new ColumnInfo { Name = name, Column = col });
                }
                return colInfos;
            };
            #endregion
        }

        private void Form_Load(object sender, EventArgs e)
        {
            tabGeneral.BackColor = tabAdvanced.BackColor = this.BackColor;
        }

        private async void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要重置表格配置吗？重置后此表格的配置会全部删除") != DialogResult.Yes) return;

            await _gridControlPlus.SettingManager.DeleteAllAsync();
            this.Close();
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            await _gridControlPlus.SettingManager.SaveCurrentAsync();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void btnRestoreDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要恢复表格默认配置吗？恢复会丢失当前配置") != DialogResult.Yes) return;

            await _gridControlPlus.SettingManager.LoadDefaultAsync();
            MessageBoxHelper.ShowMessage("恢复表格默认配置成功,请点保存确认");
            this.Close();
        }

        private async void btnSaveDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要把当前配置设为默认配置吗？") != DialogResult.Yes) return;
            await _gridControlPlus.SettingManager.SaveDefaultAsync();
            MessageBoxHelper.ShowMessage("默认配置设置成功");
        }
    }
}